<template>
    <div>
        <!-- 顶部滑动条 -->
        <div id="slider" class="mui-slider">
            <div id="sliderSegmentedControl" class="mui-scroll-wrapper mui-slider-indicator mui-segmented-control mui-segmented-control-inverted">
                <div class="mui-scroll">
                    <a :class="['mui-control-item',item.imgTypeId===0?'mui-active':''] " v-for="item in cates" :key="item.imgTypeId" @tap="getPhotoListByCateId(item.imgTypeId)">
                        {{ item.imgTypeName }}
                    </a>
                </div>
            </div>
        </div>

        <!-- 图片列表区域 -->
        <ul class="photo-list">
            <li v-for="item in list" :key="item.imageId" @click="clickPhotoInfo(item.imageId)">
                <img v-lazy="item.imageUrl">
                <div class="info">
                    <h1 class="info-title">
                        {{item.imageTitle}}
                    </h1>
                    <div class="info-body">
                        {{item.content}}
                    </div>
                </div>
            </li>
        </ul>


    </div>
</template>

<script>
    //1.导入 mui 的js 文件
    import mui from '../../lib/mui/js/mui.min.js'

    export default {
        data() {
            return {
                cates:[],//所有分类的列表数组
                list:[]//图片列表
            };
        },
        created() {
            this.getAllCategory();
            // 默认进入页面，就主动请求 所有图片列表的数据
            this.getPhotoListByCateId(0);
        },
        mounted() {  //当组建中的 DOM 结构被渲染好并被放到页面中之后，会执行这个钩子函数
            //如果要操作元素了，最好在 mounted 里面，因为这时候的 DOM 元素是最新的
            //2.初始化滑动控件
            mui('.mui-scroll-wrapper').scroll({
                deceleration: 0.0005 //flick 减速系数，系数越大，滚动速度越慢，滚动距离越小，默认值0.0006
            });
        },
        methods:{
            getAllCategory() {
                this.$http.get('/api/image/getPhotoType').then(res=>{
                    if(res.body.success===true) {
                        //手动拼接出一个最完整的分类列表
                        res.body.photoType.unshift({imgTypeName:'全部',imgTypeId:0});
                        this.cates = res.body.photoType;
                    }
                })
            },
            getPhotoListByCateId(cateId) {
                this.$http.get('/api/image/getPhotolist/'+cateId).then(res => {
                    if(res.body.success === true) {
                        this.list = res.body.page.list
                    }
                });
            },
            clickPhotoInfo(id) {//点击详情时，点击次数+1
                this.$http.post("/api/image/updatePhotoClick?imageId="+id).then(res => {
                    if(res.body.size<=0) {
                        Toast("点击加载失败")
                    }else {
                        this.$router.push('/home/photoinfo/'+id);
                    }
                })
            }

        }
    }
</script>

<style lang="scss" scoped>
    * {
        touch-action: pan-y;
    }

    .photo-list{
        list-style: none;
        margin: 0;
        padding: 10px;
        padding-bottom: 0;
        li{
            background-color: #ccc;
            text-align: center;
            margin-bottom: 10px;
            box-shadow: 0 0 9px #999;
            position: relative;
            img{
                width: 100%;
                height: 400px;
                vertical-align: middle;
            }
            img[lazy="loading"] {
                width: 40px;
                height: 300px;
                margin: auto;
            }

            .info{
                color:white;
                text-align: left;
                position: absolute;
                bottom: 0;
                background-color: rgba(0,0,0,0.4);
                max-height: 93px;
                .info-title{
                    font-size: 18px;
                }
                .info-body{
                    font-size: 15px;
                }
            }
        }
    }


</style>